

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Try not to become a man of success but rather try to become a man of value. ">
  <meta name="author" content="kinzun">
  <meta name="keywords" content>
  <meta name="description" content="WebSocket 介绍什么是 WebSocket ?WebSocket 是独立的、创建在 TCP 上的协议。 Websocket 通过 HTTP/1.1 协议的101状态码进行握手。 为了创建 Websocket 连接，需要通过浏览器发出请求，之后服务器进行回应，这个过程通常称为 “握手)”（handshaking）。  HTTP 是运行在 TCP 协议传输层的应用协议 ，WebSocket 是">
<meta name="keywords" content="Python">
<meta property="og:type" content="article">
<meta property="og:title" content="Python GeventWebSocket">
<meta property="og:url" content="https://kinzun.github.io/2019/04/01/Python GeventWebSocket/index.html">
<meta property="og:site_name" content="kinzun">
<meta property="og:description" content="WebSocket 介绍什么是 WebSocket ?WebSocket 是独立的、创建在 TCP 上的协议。 Websocket 通过 HTTP/1.1 协议的101状态码进行握手。 为了创建 Websocket 连接，需要通过浏览器发出请求，之后服务器进行回应，这个过程通常称为 “握手)”（handshaking）。  HTTP 是运行在 TCP 协议传输层的应用协议 ，WebSocket 是">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://kinzun.github.io/images/python/python_socker1.png">
<meta property="og:image" content="https://kinzun.github.io/images/python/python_socker2.png">
<meta property="og:image" content="https://kinzun.github.io/images/python/python_socker3.png">
<meta property="og:updated_time" content="2019-05-12T17:17:45.631Z">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Python GeventWebSocket">
<meta name="twitter:description" content="WebSocket 介绍什么是 WebSocket ?WebSocket 是独立的、创建在 TCP 上的协议。 Websocket 通过 HTTP/1.1 协议的101状态码进行握手。 为了创建 Websocket 连接，需要通过浏览器发出请求，之后服务器进行回应，这个过程通常称为 “握手)”（handshaking）。  HTTP 是运行在 TCP 协议传输层的应用协议 ，WebSocket 是">
<meta name="twitter:image" content="https://kinzun.github.io/images/python/python_socker1.png">
  
  <title>Python GeventWebSocket - kinzun</title>

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css">


  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css">
  <link rel="stylesheet" href="/lib/hint/hint.min.css">

  
    
    
      
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css">
    
  

  
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">

<link rel="stylesheet" href="/css/main.css">

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"kinzun.github.io","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script src="/js/utils.js"></script>
  <script src="/js/color-schema.js"></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>kinzun</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Python GeventWebSocket">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2019-04-01 10:38" pubdate>
        2019年4月1日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      10k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      33 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Python GeventWebSocket</h1>
            
            <div class="markdown-body">
              <h2 id="WebSocket-介绍"><a href="#WebSocket-介绍" class="headerlink" title="WebSocket 介绍"></a>WebSocket 介绍</h2><h2 id="什么是-WebSocket"><a href="#什么是-WebSocket" class="headerlink" title="什么是 WebSocket ?"></a>什么是 WebSocket ?</h2><p>WebSocket 是独立的、创建在 TCP 上的协议。</p>
<p>Websocket 通过 <a href="https://zh.wikipedia.org/wiki/HTTP" target="_blank" rel="noopener">HTTP</a>/1.1 协议的101<a href="https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81" target="_blank" rel="noopener">状态码</a>进行握手。</p>
<p>为了创建 <code>Websocket</code> 连接，需要通过浏览器发出请求，之后服务器进行回应，这个过程通常称为 “<a href="https://zh.wikipedia.org/wiki/%E6%8F%A1%E6%89%8B_(%E6%8A%80%E6%9C%AF" target="_blank" rel="noopener">握手</a>)”（<code>handshaking</code>）。</p>
<ul>
<li><code>HTTP</code> 是运行在 <code>TCP</code> 协议传输层的应用协议 ，<code>WebSocket</code> 是通过 HTTP 协议协商如何连接，然后独立运行在 <code>TCP</code> 协议传输层上的 应用协议。</li>
<li><code>WebSocket</code> 是一个持久化的协议，相对于 <code>HTTP</code> 这种非持久的协议来说。</li>
</ul>
<h2 id="使用-Python-scoket"><a href="#使用-Python-scoket" class="headerlink" title="使用 Python scoket"></a>使用 Python scoket</h2><ul>
<li><p>服务端暂时只是获取 <code>HTTP</code> 和 <code>Websocket</code> 请求头。</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> socket<br><span class="hljs-keyword">import</span> base64<br><span class="hljs-keyword">import</span> hashlib<br><br>HOST = <span class="hljs-string">"127.0.0.1"</span><br>PORT = <span class="hljs-number">9999</span><br><br><span class="hljs-keyword">with</span> socket.socket(socket.AF_INET, socket.SOCK_STREAM) <span class="hljs-keyword">as</span> s:<br>    <span class="hljs-comment"># 端口重用</span><br>    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="hljs-number">1</span>)<br>    s.bind((HOST, PORT))<br>    s.listen()<br>    conn, addr = s.accept()<br>    <span class="hljs-comment"># 获取客户端的握手信息</span><br>    <span class="hljs-keyword">with</span> conn:<br>        <span class="hljs-comment"># print('Connected by', addr)</span><br>        <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:<br>            data = conn.recv(<span class="hljs-number">1024</span>)<br>            frmat = data.decode()<br>            print(frmat)<br>            <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> data: <span class="hljs-keyword">break</span><br>            conn.sendall(data)<br></code></pre></div></td></tr></table></figure>
</li>
<li><p>WebSocket 客户端代码，只做连接</p>
<figure class="highlight javascript"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs javascript">&lt;script type=<span class="hljs-string">"text/javascript"</span>&gt; <br>	<span class="hljs-keyword">var</span> ws = <span class="hljs-keyword">new</span> WebSocket(<span class="hljs-string">"ws://127.0.0.1:9999"</span>)<br><br>    ws.onmessage = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) </span>&#123;<br>        <span class="hljs-built_in">console</span>.log(event.data);<br>    &#125;;<br><span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span><br></code></pre></div></td></tr></table></figure>
</li>
</ul>
<p>以下为获取<strong>请求头</strong>。</p>
<p>原始 <code>HTTP</code> 请求头（ 上图 ） 对比 <code>WebSocket</code> </p>
<p><img src="/images/python/python_socker1.png" srcset="/img/loading.gif" lazyload alt="image-20190322203358514"></p>
<p>+=</p>
<p><img src="/images/python/python_socker2.png" srcset="/img/loading.gif" lazyload alt="image-20190322203504577"></p>
<p>获取 <code>Sec-WebSocket-Key</code> 的值</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_headers</span><span class="hljs-params">(data)</span>:</span><br>    header_dict = &#123;&#125;<br>    header_str = data.decode(<span class="hljs-string">"utf8"</span>)<br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> header_str.split(<span class="hljs-string">"\r\n"</span>):<br>        <span class="hljs-keyword">if</span> str(i).startswith(<span class="hljs-string">"Sec-WebSocket-Key"</span>):<br>            header_dict[<span class="hljs-string">"Sec-WebSocket-Key"</span>] = i.split(<span class="hljs-string">":"</span>)[<span class="hljs-number">1</span>].strip()<br><br>    <span class="hljs-keyword">return</span> header_dict<br></code></pre></div></td></tr></table></figure>
<p> 请求和响应的<strong>握手</strong>信息需要遵循规则：</p>
<ul>
<li>从请求<strong>握手</strong>信息中提取 <code>S·ec-WebSocket-Key</code> 的值</li>
<li>利用 <code>magic_string</code> 和 <code>Sec-WebSocket-Ke</code>y  的值 ，进行 <code>hmac1(sha1)</code> 加密，再进行 <code>base64</code> 加密</li>
<li>将结果做为 <code>“ Sec-WebSocket-Accept”</code> 头的值，返回给客户端。</li>
</ul>
<p>magic_string<code>特殊字符串 「</code> 258EAFA5-E914-47DA-95CA-C5AB0DC85B11`  」</p>
<p>再来介绍一下ready state 表示的四种状态</p>
<ul>
<li>CONNECTING (0)：表示还没建立连接； </li>
<li>OPEN (1)： 已经建立连接，可以进行通讯； </li>
<li>CLOSING (2)：通过关闭握手，正在关闭连接； </li>
<li>CLOSED (3)：连接已经关闭或无法打开；</li>
</ul>
<hr>
<p><img src="/images/python/python_socker3.png" srcset="/img/loading.gif" lazyload alt="2019-03-22 at 10.41 PM"></p>
<p>客户端 接受客户端信息解密，流程</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><br>hashstr = <span class="hljs-string">b'\x81\x8b\xf1\x9c*=\x99\xf9FQ\x9e\xb0]R\x83\xf0N'</span><br><br><span class="hljs-comment"># 将第二个字节也就是 \x83 第9-16位 进行与127进行位运算</span><br>payload = hashstr[<span class="hljs-number">1</span>] &amp; <span class="hljs-number">127</span><br>print(payload)<br><span class="hljs-keyword">if</span> payload == <span class="hljs-number">127</span>:<br>    extend_payload_len = hashstr[<span class="hljs-number">2</span>:<span class="hljs-number">10</span>]<br>    mask = hashstr[<span class="hljs-number">10</span>:<span class="hljs-number">14</span>]<br>    decoded = hashstr[<span class="hljs-number">14</span>:]<br><span class="hljs-comment"># 当位运算结果等于127时,则第3-10个字节为数据长度</span><br><span class="hljs-comment"># 第11-14字节为mask 解密所需字符串</span><br><span class="hljs-comment"># 则数据为第15字节至结尾</span><br><br><span class="hljs-keyword">if</span> payload == <span class="hljs-number">126</span>:<br>    extend_payload_len = hashstr[<span class="hljs-number">2</span>:<span class="hljs-number">4</span>]<br>    mask = hashstr[<span class="hljs-number">4</span>:<span class="hljs-number">8</span>]<br>    decoded = hashstr[<span class="hljs-number">8</span>:]<br><span class="hljs-comment"># 当位运算结果等于126时,则第3-4个字节为数据长度</span><br><span class="hljs-comment"># 第5-8字节为mask 解密所需字符串</span><br><span class="hljs-comment"># 则数据为第9字节至结尾</span><br><br><br><span class="hljs-keyword">if</span> payload &lt;= <span class="hljs-number">125</span>:<br>    extend_payload_len = <span class="hljs-keyword">None</span><br>    mask = hashstr[<span class="hljs-number">2</span>:<span class="hljs-number">6</span>]<br>    decoded = hashstr[<span class="hljs-number">6</span>:]<br><br><span class="hljs-comment"># 当位运算结果小于等于125时,则这个数字就是数据的长度</span><br><span class="hljs-comment"># 第3-6字节为mask 解密所需字符串</span><br><span class="hljs-comment"># 则数据为第7字节至结尾</span><br><br>str_byte = bytearray()<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(len(decoded)):<br>    byte = decoded[i] ^ mask[i % <span class="hljs-number">4</span>]<br>    str_byte.append(byte)<br><br>print(str_byte.decode(<span class="hljs-string">"utf8"</span>))<br></code></pre></div></td></tr></table></figure>
<p>加密</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> struct<br>msg_bytes = <span class="hljs-string">"hello"</span>.encode(<span class="hljs-string">"utf8"</span>)<br>token = <span class="hljs-string">b"\x81"</span><br>length = len(msg_bytes)<br><br><span class="hljs-keyword">if</span> length &lt; <span class="hljs-number">126</span>:<br>    token += struct.pack(<span class="hljs-string">"B"</span>, length)<br><span class="hljs-keyword">elif</span> length == <span class="hljs-number">126</span>:<br>    token += struct.pack(<span class="hljs-string">"!BH"</span>, <span class="hljs-number">126</span>, length)<br><span class="hljs-keyword">else</span>:<br>    token += struct.pack(<span class="hljs-string">"!BQ"</span>, <span class="hljs-number">127</span>, length)<br><br>msg = token + msg_bytes<br><br>print(msg)<br></code></pre></div></td></tr></table></figure>
<h2 id="完整代码示例"><a href="#完整代码示例" class="headerlink" title="完整代码示例"></a>完整代码示例</h2><p>不再赘述</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">import</span> socket<br><span class="hljs-keyword">import</span> base64<br><span class="hljs-keyword">import</span> hashlib<br><span class="hljs-keyword">import</span> struct<br><br>HOST = <span class="hljs-string">"127.0.0.1"</span><br>PORT = <span class="hljs-number">9999</span><br><br><span class="hljs-comment"># 服务器回应,响应头</span><br>response_tpl = <span class="hljs-string">"HTTP/1.1 101 Switching Protocols\r\n"</span> \<br>               <span class="hljs-string">"Upgrade:websocket\r\n"</span> \<br>               <span class="hljs-string">"Connection: Upgrade\r\n"</span> \<br>               <span class="hljs-string">"Sec-WebSocket-Accept: %s\r\n"</span> \<br>               <span class="hljs-string">"WebSocket-Location: ws://127.0.0.1:9999\r\n\r\n"</span><br><br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_headers</span><span class="hljs-params">(data)</span>:</span><br>    <span class="hljs-comment"># 获取 sec-websocket-key</span><br>    header_dict = &#123;&#125;<br>    header_str = data.decode(<span class="hljs-string">"utf8"</span>)<br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> header_str.split(<span class="hljs-string">"\r\n"</span>):<br><br>        <span class="hljs-keyword">if</span> str(i).startswith(<span class="hljs-string">"Sec-WebSocket-Key"</span>):<br>            header_dict[<span class="hljs-string">"Sec-WebSocket-Key"</span>] = i.split(<span class="hljs-string">":"</span>)[<span class="hljs-number">1</span>].strip()<br><br>    <span class="hljs-keyword">return</span> header_dict<br><br><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">decrypt_client_info</span><span class="hljs-params">(msg)</span>:</span><br>    <span class="hljs-string">'''解密客户端发送的信息'''</span><br><br>    hashstr = msg<br>    <span class="hljs-comment"># 将第二个字节也就是 \x83 第9-16位 进行与127进行位运算</span><br>    payload = hashstr[<span class="hljs-number">1</span>] &amp; <span class="hljs-number">127</span><br>    print(payload)<br>    <span class="hljs-keyword">if</span> payload == <span class="hljs-number">127</span>:<br>        extend_payload_len = hashstr[<span class="hljs-number">2</span>:<span class="hljs-number">10</span>]<br>        mask = hashstr[<span class="hljs-number">10</span>:<span class="hljs-number">14</span>]<br>        decoded = hashstr[<span class="hljs-number">14</span>:]<br>    <span class="hljs-comment"># 当位运算结果等于127时,则第3-10个字节为数据长度</span><br>    <span class="hljs-comment"># 第11-14字节为mask 解密所需字符串</span><br>    <span class="hljs-comment"># 则数据为第15字节至结尾</span><br><br>    <span class="hljs-keyword">if</span> payload == <span class="hljs-number">126</span>:<br>        extend_payload_len = hashstr[<span class="hljs-number">2</span>:<span class="hljs-number">4</span>]<br>        mask = hashstr[<span class="hljs-number">4</span>:<span class="hljs-number">8</span>]<br>        decoded = hashstr[<span class="hljs-number">8</span>:]<br>    <span class="hljs-comment"># 当位运算结果等于126时,则第3-4个字节为数据长度</span><br>    <span class="hljs-comment"># 第5-8字节为mask 解密所需字符串</span><br>    <span class="hljs-comment"># 则数据为第9字节至结尾</span><br><br>    <span class="hljs-keyword">if</span> payload &lt;= <span class="hljs-number">125</span>:<br>        extend_payload_len = <span class="hljs-keyword">None</span><br>        mask = hashstr[<span class="hljs-number">2</span>:<span class="hljs-number">6</span>]<br>        decoded = hashstr[<span class="hljs-number">6</span>:]<br><br>    <span class="hljs-comment"># 当位运算结果小于等于125时,则这个数字就是数据的长度</span><br>    <span class="hljs-comment"># 第3-6字节为mask 解密所需字符串</span><br>    <span class="hljs-comment"># 则数据为第7字节至结尾</span><br><br>    str_byte = bytearray()<br><br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(len(decoded)):<br>        byte = decoded[i] ^ mask[i % <span class="hljs-number">4</span>]<br>        str_byte.append(byte)<br><br>    <span class="hljs-keyword">return</span> str_byte.decode(<span class="hljs-string">"utf8"</span>)<br><br><br><span class="hljs-comment"># 发送消息</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_msg</span><span class="hljs-params">(msg, conn)</span>:</span><br>    <span class="hljs-comment"># 服务端回应，防止粘包现象 使用了 struct 模块</span><br>    msg_bytes = msg.encode(<span class="hljs-string">"utf8"</span>)<br>    token = <span class="hljs-string">b"\x81"</span><br>    length = len(msg_bytes)<br><br>    <span class="hljs-keyword">if</span> length &lt; <span class="hljs-number">126</span>:<br>        token += struct.pack(<span class="hljs-string">"B"</span>, length)<br>    <span class="hljs-keyword">elif</span> length == <span class="hljs-number">126</span>:<br>        token += struct.pack(<span class="hljs-string">"!BH"</span>, <span class="hljs-number">126</span>, length)<br>    <span class="hljs-keyword">else</span>:<br>        token += struct.pack(<span class="hljs-string">"!BQ"</span>, <span class="hljs-number">127</span>, length)<br>    msg = token + msg_bytes<br>    conn.send(msg)<br><br><br><span class="hljs-keyword">with</span> socket.socket(socket.AF_INET, socket.SOCK_STREAM) <span class="hljs-keyword">as</span> s:<br>    <span class="hljs-comment"># 端口重用</span><br>    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, <span class="hljs-number">1</span>)<br>    s.bind((HOST, PORT))<br>    s.listen()<br>    conn, addr = s.accept()<br>    <span class="hljs-comment"># 获取客户端的握手信息</span><br>    <span class="hljs-keyword">with</span> conn:<br>        <span class="hljs-comment"># print('Connected by', addr)</span><br>        data = conn.recv(<span class="hljs-number">1024</span>)<br>        headers = get_headers(data)  <span class="hljs-comment"># 提取请求头信息</span><br>        <span class="hljs-comment"># 对请求头中的 sec-websocket-key 进行加密</span><br>        <span class="hljs-comment"># 固定特殊字符串</span><br>        magic_string = <span class="hljs-string">'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'</span><br>        value = headers[<span class="hljs-string">'Sec-WebSocket-Key'</span>] + magic_string<br>        print(value)<br>        ac = base64.b64encode(hashlib.sha1(value.encode(<span class="hljs-string">'utf-8'</span>)).digest())<br><br>        response_str = response_tpl % (ac.decode(<span class="hljs-string">'utf-8'</span>))<br>        <span class="hljs-comment"># 响应握手信息</span><br>        conn.send(response_str.encode(<span class="hljs-string">"utf8"</span>))<br>        <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:<br>            msg = conn.recv(<span class="hljs-number">8096</span>)<br>            print(decrypt_client_info(msg))<br>            send_msg(<span class="hljs-string">'你好啊发送发送发送'</span>, conn)<br></code></pre></div></td></tr></table></figure>
<p>HTML </p>
<figure class="highlight javascript"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs javascript">&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head lang="en"&gt;<br>    &lt;meta charset="UTF-8"&gt;<br>    &lt;title&gt;&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;div&gt;<br>    &lt;input type="text" id="txt"/&gt;<br>    &lt;input type="button" id="btn" value="发送消息" onclick="sendMsg();"/&gt;<br>    &lt;input type="button" id="close" value="关闭连接" onclick="closeConn();"/&gt;<br>&lt;/div&gt;<br>&lt;div id="content"&gt;&lt;/div&gt;<br><br>&lt;script type="text/javascript"&gt;<br>    var socket = new WebSocket("ws://127.0.0.1:9999");<br><br>    socket.onopen = function () &#123;<br>        /* 与服务器端连接成功后，自动执行 */<br><br>        var newTag = document.createElement('div');<br>        newTag.innerHTML = "【连接成功】";<br>        document.getElementById('content').appendChild(newTag);<br>    &#125;;<br><br>    socket.onmessage = function (event) &#123;<br>        /* 服务器端向客户端发送数据时，自动执行 */<br>        var response = event.data;<br>        var newTag = document.createElement('div');<br>        newTag.innerHTML = response;<br>        document.getElementById('content').appendChild(newTag);<br>    &#125;;<br><br>    socket.onclose = function (event) &#123;<br>        /* 服务器端主动断开连接时，自动执行 */<br>        var newTag = document.createElement('div');<br>        newTag.innerHTML = "【关闭连接】";<br>        document.getElementById('content').appendChild(newTag);<br>    &#125;;<br>    // socket.addEventListener('message', function (event) &#123;<br>    //     console.log('Message from server ', event.data);<br>    //<br>    // &#125;);<br><br><br>    function sendMsg() &#123;<br>        var txt = document.getElementById('txt');<br>        socket.send(txt.value);<br>        txt.value = "";<br>    &#125;<br><br>    function closeConn() &#123;<br>        socket.close();<br>        var newTag = document.createElement('div');<br>        newTag.innerHTML = "【关闭连接】";<br>        document.getElementById('content').appendChild(newTag);<br>    &#125;<br><br>&lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br></code></pre></div></td></tr></table></figure>
<h2 id="gevent-实现-websocker-demo-版本"><a href="#gevent-实现-websocker-demo-版本" class="headerlink" title="gevent 实现 websocker demo 版本"></a>gevent 实现 websocker demo 版本</h2><figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><br><span class="hljs-keyword">import</span> json<br><span class="hljs-keyword">import</span> os<br><span class="hljs-keyword">import</span> sys<br><span class="hljs-keyword">from</span> gevent <span class="hljs-keyword">import</span> monkey<br><br>monkey.patch_all()<br>BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))<br>sys.path.append(BASE_DIR)<br><br><span class="hljs-keyword">from</span> pymongo <span class="hljs-keyword">import</span> MongoClient<br><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> render_template, Flask<br><span class="hljs-keyword">from</span> werkzeug.debug <span class="hljs-keyword">import</span> DebuggedApplication<br><br><span class="hljs-keyword">from</span> geventwebsocket <span class="hljs-keyword">import</span> WebSocketServer, WebSocketApplication, Resource<br><br><span class="hljs-keyword">from</span> mechat.blueprints.ai_baidu <span class="hljs-keyword">import</span> Baidu_AI<br><br>baidu_ai = Baidu_AI()<br><br>mgclient = MongoClient(<span class="hljs-string">"127.0.0.1"</span>, <span class="hljs-number">27017</span>)<br>MONGO_DB = mgclient[<span class="hljs-string">"chat_mess_room"</span>]<br><br>flask_app = Flask(__name__)<br>flask_app.debug = <span class="hljs-keyword">True</span><br><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ChatApplication</span><span class="hljs-params">(WebSocketApplication)</span>:</span><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_open</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">'connect'</span>)<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_message</span><span class="hljs-params">(self, message)</span>:</span><br>        <span class="hljs-keyword">if</span> message <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:<br>            <span class="hljs-keyword">return</span><br><br>        message = json.loads(message)<br>        <span class="hljs-comment"># a = &#123;'msg_type': 'message', 'nickname': 'unknown', 'message': '你啊'&#125;</span><br><br>        <span class="hljs-keyword">if</span> message[<span class="hljs-string">'msg_type'</span>] == <span class="hljs-string">'message'</span>:<br>            im = message[<span class="hljs-string">'message'</span>]<br>            tuningre = baidu_ai.tuning(im)<br>            cur_time = message.get(<span class="hljs-string">'cur_time'</span>)<br>            the_robot = &#123;<span class="hljs-string">'msg_type'</span>: <span class="hljs-string">'message'</span>, <span class="hljs-string">'nickname'</span>: <span class="hljs-string">'图灵机器人的爸爸'</span>, <span class="hljs-string">'message'</span>: tuningre, <span class="hljs-string">'the_robot'</span>: <span class="hljs-string">"robot"</span>,<br>                         <span class="hljs-string">'cur_time'</span>: cur_time&#125;<br><br>            self.broadcast(message)<br>            self.broadcast(the_robot)<br>            MONGO_DB.groups.insert_many([message, the_robot])<br><br><br><br>        <span class="hljs-keyword">elif</span> message[<span class="hljs-string">'msg_type'</span>] == <span class="hljs-string">'update_clients'</span>:<br>            self.send_client_list(message)<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_client_list</span><span class="hljs-params">(self, message)</span>:</span><br>        current_client = self.ws.handler.active_client<br>        current_client.nickname = message[<span class="hljs-string">'nickname'</span>]<br><br>        self.ws.send(json.dumps(&#123;<br>            <span class="hljs-string">'msg_type'</span>: <span class="hljs-string">'update_clients'</span>,<br>            <span class="hljs-string">'clients'</span>: [<br>                getattr(client, <span class="hljs-string">'nickname'</span>, <span class="hljs-string">'anonymous'</span>)<br>                <span class="hljs-keyword">for</span> client <span class="hljs-keyword">in</span> self.ws.handler.server.clients.values()<br>            ]<br>        &#125;))<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">broadcast</span><span class="hljs-params">(self, message)</span>:</span><br>        <span class="hljs-keyword">for</span> client <span class="hljs-keyword">in</span> self.ws.handler.server.clients.values():<br>            <span class="hljs-comment"># client.ws.send(json.dumps(&#123;</span><br>            <span class="hljs-comment">#     'msg_type': 'message',</span><br>            <span class="hljs-comment">#     'nickname': message['nickname'],</span><br>            <span class="hljs-comment">#     'message': message['message']</span><br>            <span class="hljs-comment"># &#125;))</span><br>            client.ws.send(json.dumps(message))<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_close</span><span class="hljs-params">(self, reason)</span>:</span><br>        print(<span class="hljs-string">'Connectiion closed'</span>)<br><br><br><span class="hljs-meta">@flask_app.route('/')</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">home</span><span class="hljs-params">()</span>:</span><br>    te = [&#123;<span class="hljs-string">"nickname"</span>: <span class="hljs-string">"baozi"</span>, <span class="hljs-string">"body"</span>: <span class="hljs-string">'你的名字叫啥'</span>&#125;,<br>          &#123;<span class="hljs-string">"nickname"</span>: <span class="hljs-string">"baozi"</span>, <span class="hljs-string">"body"</span>: <span class="hljs-string">'做个自我介绍啊'</span>&#125;,<br>          &#123;<span class="hljs-string">"nickname"</span>: <span class="hljs-string">"baozi"</span>, <span class="hljs-string">"body"</span>: <span class="hljs-string">'发图啊'</span>&#125;]<br><br>    re = list(MONGO_DB.groups.find(&#123;&#125;, &#123;<span class="hljs-string">"_id"</span>: <span class="hljs-number">0</span>&#125;))<br><br>    <span class="hljs-comment"># return render_template("home2.html")</span><br>    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">"home2.html"</span>, messages=re)<br><br><br><span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:<br>    WebSocketServer((<span class="hljs-string">'192.168.1.2'</span>, <span class="hljs-number">9999</span>), Resource([<br>        (<span class="hljs-string">'^/chat'</span>, ChatApplication),<br>        (<span class="hljs-string">'^/.*'</span>, DebuggedApplication(flask_app))<br>    ]), debug=<span class="hljs-keyword">False</span>).serve_forever()<br></code></pre></div></td></tr></table></figure>
<p>V2</p>
<figure class="highlight python"><table><tr><td class="gutter hljs"><div class="hljs code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></div></td><td class="code"><div class="hljs code-wrapper"><pre><code class="hljs python"><span class="hljs-keyword">from</span> __future__ <span class="hljs-keyword">import</span> print_function<br><br><span class="hljs-keyword">import</span> json<br><br><span class="hljs-keyword">from</span> gevent <span class="hljs-keyword">import</span> monkey<br><br>monkey.patch_all()<br><br><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask, render_template<br><span class="hljs-keyword">from</span> werkzeug.debug <span class="hljs-keyword">import</span> DebuggedApplication<br><br><span class="hljs-keyword">from</span> geventwebsocket <span class="hljs-keyword">import</span> WebSocketServer, WebSocketApplication, Resource<br><br>websocket = Flask(__name__)<br>websocket.debug = <span class="hljs-keyword">True</span><br><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ChatApplication</span><span class="hljs-params">(WebSocketApplication)</span>:</span><br>    <span class="hljs-string">''' webSocket 消息发送'''</span><br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_open</span><span class="hljs-params">(self)</span>:</span><br>        print(<span class="hljs-string">"Some client connected!"</span>)<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_message</span><span class="hljs-params">(self, message)</span>:</span><br>        <span class="hljs-keyword">if</span> message <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span>:<br>            <span class="hljs-keyword">return</span><br><br>        message = json.loads(message)<br><br>        <span class="hljs-keyword">if</span> message.get(<span class="hljs-string">'msg_type'</span>) == <span class="hljs-string">'send_'</span>:<br>            print(message)<br>            self.share_music_friend(message)<br><br>        <span class="hljs-keyword">elif</span> message.get(<span class="hljs-string">'msg_type'</span>) == <span class="hljs-string">'accept_'</span>:<br>            self.accept_client_list(message)<br>        <span class="hljs-keyword">else</span>:<br>            <span class="hljs-keyword">pass</span><br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">share_music_friend</span><span class="hljs-params">(self, message)</span>:</span><br>        current_client = self.ws.handler.active_client<br>        <span class="hljs-comment"># current_client.from_id = message.get('toy_id')</span><br>        current_client.from_id = message.get(<span class="hljs-string">'toy_id'</span>, message.get(<span class="hljs-string">"from_user"</span>))<br><br>        <span class="hljs-keyword">for</span> client <span class="hljs-keyword">in</span> self.ws.handler.server.clients.values():<br>            <span class="hljs-keyword">if</span> getattr(client, <span class="hljs-string">'from_id'</span>, <span class="hljs-string">'anonymous'</span>) == message[<span class="hljs-string">'to_user'</span>]:<br>                client.ws.send(json.dumps(message))<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">accept_client_list</span><span class="hljs-params">(self, message)</span>:</span><br>        print(message)<br>        current_client = self.ws.handler.active_client<br>        current_client.from_id = message.get(<span class="hljs-string">'toy_id'</span>, message.get(<span class="hljs-string">"from_user"</span>))<br><br>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">on_close</span><span class="hljs-params">(self, reason)</span>:</span><br>        print(<span class="hljs-string">"Connection closed!"</span>)<br><br><br><span class="hljs-meta">@websocket.route('/toy')</span><br><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">toy</span><span class="hljs-params">()</span>:</span><br>    <span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'toy.html'</span>)<br><br><br>WebSocketServer(<br>    (<span class="hljs-string">"192.168.1.2"</span>, <span class="hljs-number">8001</span>),<br><br>    Resource([<br>        (<span class="hljs-string">'^/sendone'</span>, ChatApplication),<br>        (<span class="hljs-string">'^/.*'</span>, DebuggedApplication(websocket))<br>    ]),<br><br>    debug=<span class="hljs-keyword">False</span><br>).serve_forever()<br></code></pre></div></td></tr></table></figure>
<p><a href="https://zh.wikipedia.org/wiki/WebSocket" target="_blank" rel="noopener">WebSocket</a></p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers" target="_blank" rel="noopener">Writing WebSocket servers</a></p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket" target="_blank" rel="noopener"><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket" target="_blank" rel="noopener">https://developer.mozilla.org/en-US/docs/Web/API/WebSocket</a></a></p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Python/">Python</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2019/04/10/python-协程-异步-爬虫对比/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">线程，协程对比和 Python 爬虫使用</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2018/12/01/浅显理解 Pyhton 中闭包/">
                        <span class="hidden-mobile">浅显理解 Pyhton 中闭包</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
